package com.gewu.pm.ui.fragment.salesperson;

import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.app.AppFragment;
import com.gewu.pm.ui.activity.customer.SalesPersonDetailsActivity;
import com.gewu.pm.ui.adapter.customer.SalespersonVisitAdapter;
import com.hjq.base.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 业务员详情 - 拜访Tab
 * 
 * 功能：
 * 1. 显示日期范围（可筛选）
 * 2. 显示拜访记录列表
 * 3. 支持点击拜访Item
 */
public final class SalespersonVisitFragment extends AppFragment<SalesPersonDetailsActivity> 
        implements BaseAdapter.OnItemClickListener {

    private TextView mRecordCountView;
    private TextView mDateRangeView;
    private RecyclerView mRecyclerView;
    
    private SalespersonVisitAdapter mAdapter;
    private List<String> mVisitList;

    /**
     * 创建Fragment实例
     */
    public static SalespersonVisitFragment newInstance() {
        return new SalespersonVisitFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.salesperson_visit_fragment;
    }

    @Override
    protected void initView() {
        mRecordCountView = findViewById(R.id.tv_record_count);
        mDateRangeView = findViewById(R.id.tv_date_range);
        mRecyclerView = findViewById(R.id.rv_visit_list);
        
        // 设置日期范围（默认显示当前日期范围）
        String startDate = "2025-8-18";
        String endDate = "2025-8-18";
        mDateRangeView.setText(getString(R.string.date_range_format, startDate, endDate));
        
        // 点击日期范围，弹出日期选择器
        mDateRangeView.setOnClickListener(v -> showDateRangePicker());
        
        // 设置RecyclerView
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new SalespersonVisitAdapter(getContext());
        mAdapter.setOnItemClickListener(this);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        // 初始化测试数据（4条拜访记录）
        mVisitList = new ArrayList<>();
        mVisitList.add("2025-08-12");
        mVisitList.add("2025-08-12");
        mVisitList.add("2025-08-12");
        mVisitList.add("2025-08-12");
        
        loadData();
    }

    /**
     * 加载数据
     */
    private void loadData() {
        mAdapter.setData(mVisitList);
        updateRecordCount();
    }

    /**
     * 更新记录数
     */
    private void updateRecordCount() {
        int count = mAdapter.getCount();
        mRecordCountView.setText(getString(R.string.record_count_format, count));
    }

    /**
     * 显示日期范围选择器
     */
    private void showDateRangePicker() {
        // TODO: 弹出日期选择对话框
        toast("点击日期范围选择");
    }

    @Override
    public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
        String visitDate = mAdapter.getItem(position);
        toast("点击了：" + visitDate + " 的拜访记录");
        // TODO: 跳转到拜访详情页面
    }
}

